package GUILayer;

import java.awt.EventQueue;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.Label;


public class Login
{
	private JFrame frame;
	private JTextField userField;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() 
	{
		frame = new JFrame("ParaManagement");
		frame.setBounds(100, 100, 341, 237);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		user();
		userField = new JTextField();
		userField.setBounds(115, 30, 110, 22);
		frame.getContentPane().add(userField);

		password();
		passwordField = new JPasswordField();
		passwordField.setBounds(115, 70, 110, 22);
		frame.getContentPane().add(passwordField);
		
		final JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String user = userField.getText();
				char[] password = passwordField.getPassword();
				if (isLoginCorrect(user, password)) 
				{
					/* insert to create new window */
					frame.dispose();
					new Calendar();
				} 
				else 
				{
					JOptionPane.showMessageDialog(frame,
							"Beklager. Bruger eller Kodeord er forkert.", "Error Message",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnLogin.setBounds(115, 119, 110, 25);
		frame.getContentPane().add(btnLogin);
		
		KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
		@SuppressWarnings("serial")
		Action enterAction = new AbstractAction() {
		    // close the frame when the user presses escape
		    public void actionPerformed(ActionEvent e) {
		        btnLogin.doClick();
		    }
		}; 
		frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "ENTER");
		frame.getRootPane().getActionMap().put("ENTER", enterAction);
	}
	
	private void user()
	{
		Label label_user = new Label("Bruger");
		label_user.setBounds(30, 30, 70, 24);
		frame.getContentPane().add(label_user);
	}
	
	private void password()
	{
		Label label_password = new Label("Kodeord");
		label_password.setBounds(30, 70, 70, 24);
		frame.getContentPane().add(label_password);
	}
	
	private static boolean isLoginCorrect(String inputUser, char[] inputPassword) 
	{
		Boolean ok = false;
		String actualUser = "aba";
		char[] actualPassword = {'1','2','3','4'};
		if (inputUser == actualUser)
			ok = true;
		if (inputPassword.length == actualPassword.length)
			ok = true;
		else
			ok = false;
		for (int i = 0; i < inputPassword.length; i++)
			{
				if (inputPassword[i] == actualPassword[i])
					ok = true;
				else
					ok = false;
			}
		return ok;
	}
}
